package cn.picclife.mwx.manager.sys.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.FriendMapper;
import cn.picclife.mwx.manager.sys.mapper.OperatorMapper;
import cn.picclife.mwx.manager.sys.mapper.RolesMapper;
import cn.picclife.mwx.manager.sys.model.bo.RolesBo;
import cn.picclife.mwx.manager.sys.model.po.Friend;
import cn.picclife.mwx.manager.sys.model.po.Operator;
import cn.picclife.mwx.manager.sys.service.FriendService;
import cn.picclife.mwx.manager.util.uniqueseq.UniqueSeq;

/**
 * 伙伴管理业务层实现类
 * @ClassName FriendServiceImpl.java
 * @Description 
 * @author Guan JunHui
 * @Date 2017年9月20日
 */
@Service
public class FriendServiceImpl implements FriendService {
	private Log log = LogFactory.getLog(FriendServiceImpl.class);

	@Autowired
	private FriendMapper mapper;
	
	@Autowired
	private OperatorMapper operatorMapper;
	
	@Autowired
	private RolesMapper rolesMapper;
	
	/**
	 * 伙伴管理添加方法
	 */
	@Override
	public int insertFriend(Friend friend, Operator operator) {
		operator.setOptType("2");
		friend.setCreateTime(new Date());
		int flag = 0;
		String optNo = operatorMapper.queryOptId(operator.getOptNo());
		if ("".equals(optNo)) {
			return 2;//账号已经存在了
		}
		try {
			//添加用户
			operatorMapper.insert(operator);
			friend.setCompanyName(operator.getOptName());
			//添加伙伴
			mapper.insertFriend(friend);
			
			RolesBo rolesUserBo = new RolesBo();
			Long serno = UniqueSeq.getUniqueSeq("optro_id", "t_operator_roles");
			rolesUserBo.setRoleNo("Friend");
			rolesUserBo.setOptNo(operator.getOptNo());
			rolesUserBo.setCrtDate(new Date());
			rolesUserBo.setRoleId(serno);
			rolesUserBo.setCrtUser(friend.getCreateUser());
			rolesUserBo.setDataFlag("1");
			rolesUserBo.setStartDate(new Date());
			
			flag = rolesMapper.saveRolesUser(rolesUserBo);
		} catch (Exception e) {
			flag = 0;
			log.error(e);
		}
		return flag;
	}
	
	/**
	 * 伙伴管理修改方法
	 */
	@Override
	public int updateFriend(Friend friend, Operator operator){
		int flag = 0;
		friend.setEditTime(new Date());
		try {
			//修改用户
			operatorMapper.updateByOptNo(operator);
			friend.setUserId(operator.getOptNo());
			friend.setCompanyName(operator.getOptName());
			//修改伙伴
			mapper.updateFriend(friend);
			flag = 1;
		} catch (Exception e) {
			flag = 0;
			log.error(e);
		}
		return flag;
	}
	
	/**
	 * 根据主键批量删除伙伴和对应登录账号
	 */
	@Override
	public int deleteFriend(int[] ids){
		int flag = 0;
		try {
			for (int id : ids) {
				Map<String, Object>map = mapper.queryFriendAndOperator(id);
				operatorMapper.deleteById(String.valueOf(map.get("userId")));
				mapper.deleteFriend(id);
			}
			flag = 1;
		} catch (Exception e) {
			flag = 0;
			log.error(e);
		}
		return flag;
	}
	
	/**
	 * 通过主键查找伙伴的相关信息
	 */
	@Override
	public Map<String, Object> queryFriendById(int id) {
		return mapper.queryFriendAndOperator(id);
	}

	/**
	 * 分页方法
	 */
	@Override
	public List<Map<String, Object>> pageFriend(Friend friend, Operator operator) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("optNo", operator.getOptNo());
		map.put("optName", operator.getOptName());
		map.put("companyType", friend.getCompanyType());
		map.put("optStatus", operator.getOptStatus());
		map.put("applicationDate", friend.getApplicationDate());
		map.put("phone", friend.getPhone());
		//operator.getOptNo(),operator.getOptName(),friend.getCompanyType(),operator.getOptStatus(),friend.getApplicationDate(),friend.getPhone()
		int total = mapper.countFriend(map);
		friend.setTotal(total);
		int pageSize = friend.getTotal() / friend.getPageCount();
		if (friend.getTotal() % friend.getPageCount()>0) {
			pageSize++;
		}
		friend.setPageSize(pageSize);
		friend.setSkipNumber(friend.getPageCount()*(friend.getPageNumber()-1));
		map.put("skipNumber", friend.getSkipNumber());
		map.put("pageCount", friend.getPageCount());
		map.put("pageNumber", friend.getPageNumber());
		return mapper.pageFriend(map);
	}
	
	

}
